#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > rsp_mcdbterm.info <<'%EOF%'
   rsp_mcdbterm
   ------------
   Molecule:         H3+
   Wave Function:    MCSCF (CAS) = FCI / STO-3G
   Test Purpose:     B term (components) of Magnetic circular dichroism
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > rsp_mcdbterm.mol <<'%EOF%'
BASIS
STO-3G
B term (MCD) components of H3+ 

    2  1 2  X  Z      1.00D-12
        1.0   1
HC     0.000000          -0.224905893        0.000000
        1.0   1
HL     1.000000           0.899623572        0.000000
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > rsp_mcdbterm.dal <<'%EOF%'
**DALTON INPUT
.RUN RESPONSE
**WAVE FUNCTION
.HF
.MP2
.MCSCF
*SCF INPUT
.DOUBLY OCCUPIED
 1    0    0    0
*CONFIGURATION INPUT
.SPIN MULTIPLICITY
 1
.INACTIVE
 0 0 0 0
.ELECTRONS
 2
.CAS SPACE
 2 1 0 0
.SYMMETRY
 1
**RESPONSE
*QUADRATIC
.THCPP
0.00001
.THCLR
0.00001
.SINGLE RESIDUE
.ROOTS
2 2 0 0
.MCDBTE
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >rsp_mcdbterm.check
cat >>rsp_mcdbterm.check <<'%EOF%'
log=$1
#
if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

if $GREP -q "not implemented for parallel calculations" $log; then
   echo "TEST ENDED AS EXPECTED"
   exit 0
fi

#
# SCF energy
CRIT1=`$GREP "Hartree\-Fock total energy *\: * \-1\.22513194[0-9][0-9]" $log | wc -l`
TEST[1]=`expr	$CRIT1`
CTRL[1]=1
ERROR[1]="SCF ENERGY NOT CORRECT"
#
# MP2 energy
CRIT1=`$GREP "\= MP2 second order energy *\: * \-1\.24212961[0-9][0-9]" $log | wc -l`
TEST[2]=`expr	$CRIT1`
CTRL[2]=1
ERROR[2]="MP2 ENERGY NOT CORRECT"
#
# MCSCF energy
CRIT1=`$GREP "Final MCSCF energy\: * \-1\.2492926112[0-9][0-9]" $log | wc -l`
TEST[3]=`expr	$CRIT1`
CTRL[3]=1
ERROR[3]="MCSCF ENERGY NOT CORRECT"
#
# Response setup
CRIT1=`$GREP "Quadratic Response single residue calculation" $log | wc -l`
CRIT2=`$GREP "Spin of operator A \, ISPINA\= * 0" $log | wc -l`
CRIT3=`$GREP "Spin of operator B \, ISPINB\= * 0" $log | wc -l`
CRIT4=`$GREP "Spin of operator C \, \(Excitation energy\) ISPINC\= * 0" $log | wc -l`
CRIT5=`$GREP "1 B\-frequencies * (0| )\.000000(D|E)\+00" $log | wc -l`
CRIT6=`$GREP "B of Magnetic Circular Dichroism requested    : MCDCAL \=T" $log | wc -l`
TEST[4]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6`
CTRL[4]=6
ERROR[4]="RESPONSE CALCULATION NOT SET UP CORRECTLY"
#
# Transition moments
CRIT1=`$GREP "omega B\, excitation energy\, moment \: * (\-0|\-|0| )\.000000 * 1\.003438 * (\-| )1\.96682" $log | wc -l`
CRIT2=`$GREP "Excitation energy in au,    moment \: * 1\.003438 * (\-| )1\.019782" $log | wc -l`
CRIT3=`$GREP "B term contribution\: * \-1\.002864" $log | wc -l`
CRIT4=`$GREP "omega B\, excitation energy\, moment \: * (\-0|\-|0| )\.000000 * 1\.446943 * (\-| )1\.238333" $log | wc -l`
CRIT5=`$GREP "Excitation energy in au,    moment \: * 1\.446943 * (\-0|\-|0| )\.317319" $log | wc -l`
CRIT6=`$GREP "B term contribution\: * (\-0|\-)\.196473" $log | wc -l`
CRIT7=`$GREP "omega B\, excitation energy\, moment \: * (\-0|\-|0| )\.000000 * (0| )\.508268 * (\-0|\-|0| )\.000000" $log | wc -l`
CRIT8=`$GREP "Excitation energy in au,    moment \: * (0| )\.508268 * (\-0|\-|0| )\.000000" $log | wc -l`
CRIT9=`$GREP "B term contribution\: * (\-0|\-|0| )\.000000" $log | wc -l`
CRIT10=`$GREP "omega B\, excitation energy\, moment \: * (\-0|\-|0| )\.000000 * (0| )\.748121 * (\-| )1\.887384" $log | wc -l`
CRIT11=`$GREP "Excitation energy in au,    moment \: * (0| )\.748121 * (\-| )1\.231217" $log | wc -l`
CRIT12=`$GREP "B term contribution\: * (\-| )1\.161889" $log | wc -l`
TEST[5]=`expr   $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
                $CRIT7 \+ $CRIT8 \+ $CRIT9 \+ $CRIT10 \+ $CRIT11 \+ $CRIT12`
CTRL[5]=12
ERROR[5]="TRANSITION MOMENTS OR B TERM COMPONENTS NOT CORRECT"

PASSED=1
for i in 1 2 3 4 5
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done
#
if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
